package com.ruoyi.food.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.food.object.TEnterprise;

/**
 * 餐饮企业mapper
 * @author mengcc
 *
 */
@Mapper
public interface TEnterpriseMapper extends BaseMapper<TEnterprise>{
	
	public static String sql="SELECT *from t_enterprise";
	
	@Select({"<script>",
        sql,
        "where 1=1",
        "<when test='name != \"\" and name !=null'>",
        "AND name like concat('%', #{name}, '%')",
        "</when>",
        "<when test='sign != \"\" and sign !=null'>",
        "AND sign like concat('%', #{sign}, '%')",
        "</when>",
        "<when test='type != \"\" and type !=null'>",
        "AND type like concat('%', #{type}, '%')",
        "</when>",
        "</script>"})
	public List<TEnterprise> selectTenterprise(TEnterprise tEnterprise);
	
	/**
	 * 通过IS_WATER_POINT=1查询餐饮企业列表
	 * @return
	 */
	//@Select("SELECT *from t_enterprise where IS_WATER_POINT=#{isWaterPoint}")
	@Select({"<script>",
        "SELECT *from t_enterprise ",
        "WHERE 1=1",
        "<when test='isWaterPoint != 3'>",
        "AND IS_WATER_POINT=#{isWaterPoint}",
        "</when>",
        "<when test='name != \"\" and name !=null'>",
        "AND name like concat('%', #{name}, '%')",
        "</when>",
        "</script>"})
	public List<TEnterprise> selectTenterpriseByisWP(@Param("isWaterPoint")int isWaterPoint,@Param("name")String name);
	
	@Insert("insert into t_enterprise(NAME,SIGN,WORKER_NUM,TYPE,ADDRESS,INTRO,FZR_NAME,FZR_TEL,AREA,CAPACITY,CREATE_TIME,LONGITUDE,LATITUDE,IS_WATER_POINT,ACCOUNT,PASSWORD,SIGNUP_SAFE,IS_SAFE,SAFE_YEAR,STOCK_NUM)"
			+ " values(#{name},#{sign},#{workerNum},#{type},#{address},#{intro},"
			+ "#{fzrName},#{fzrTel},#{area},#{capacity},#{createTime},#{longitude},#{latitude},#{isWaterPoint},#{fzrTel},MD5(123456),#{signupSafe},#{isSafe},#{safeYear},#{stockNum})")
	public void insetTenterprise(TEnterprise tEnterprise);
	
	@Update("update t_enterprise set NAME=#{name},SIGN=#{sign},WORKER_NUM=#{workerNum},TYPE=#{type},ADDRESS=#{address},INTRO=#{intro},FZR_NAME=#{fzrName},FZR_TEL=#{fzrTel},AREA=#{area},CAPACITY=#{capacity},CREATE_TIME=#{createTime},IS_WATER_POINT=#{isWaterPoint},"
			+ "LONGITUDE=#{longitude},LATITUDE=#{latitude},SIGNUP_SAFE=#{signupSafe},IS_SAFE=#{isSafe},SAFE_YEAR=#{safeYear},STOCK_NUM=#{stockNum} where ENTERPRISE_ID=#{enterpriseId}")
	public void updateTenterprise(TEnterprise tEnterprise);
	
	@Delete("delete FROM t_enterprise where ENTERPRISE_ID=#{enterpriseId}")
	public void deleteTenterprise(long enterpriseId);
	
	@Select("select *FROM t_enterprise where ENTERPRISE_ID=#{enterpriseId}")
	public TEnterprise selectTenterpriseById(long enterpriseId);

	@Select("SELECT *from t_enterprise WHERE ACCOUNT=#{account} and `PASSWORD`=MD5(#{password})")
	public List<TEnterprise> selectTenterpriseByPassword(@Param("account")String account,@Param("password")String password);
	
	@Update("UPDATE t_enterprise set ACCOUNT=#{newAccount} ,`PASSWORD`=MD5(#{newPassword}) WHERE ACCOUNT=#{account} and `PASSWORD`=MD5(#{password})")
	public void updateAcOrPs(@Param("newAccount")String newAccount,@Param("newPassword")String newPassword,@Param("account")String account,@Param("password")String password);
	
	/**
	 * 通过是否报名放心餐饮查询
	 * @param signupSafe
	 * @param name
	 * @return
	 */
	@Select({"<script>",
        "SELECT *from t_enterprise ",
        "WHERE 1=1",
        "<when test='signupSafe != 3'>",
        "AND SIGNUP_SAFE=#{signupSafe}",
        "</when>",
        "<when test='name != \"\" and name !=null'>",
        "AND name like concat('%', #{name}, '%')",
        "</when>",
        "</script>"})
	public List<TEnterprise> selectBySafe(@Param("signupSafe")int signupSafe,@Param("name")String name);
	
	/**
	 * 查询类型包含string的餐饮企业
	 * @param string
	 * @return
	 */
	@Select("SELECT *from t_enterprise where TYPE like concat('%', #{string}, '%')")
	public List<TEnterprise> selectByContainType(String string);
	
	/**
	 * 查询类型不包含string的餐饮企业
	 * @param string
	 * @return
	 */
	@Select("SELECT *from t_enterprise where TYPE not like concat('%', #{string}, '%')")
	public List<TEnterprise> selectByNoContainType(String string);
	
	@Update("update t_enterprise set STOCK_NUM=#{stockNum} where ENTERPRISE_ID=#{enterpriseId}")
	public void updateStockNumById(@Param("enterpriseId")long enterpriseId,@Param("stockNum")int stockNum);
	
	//@Select("SELECT *from t_enterprise where SIGN LIKE concat('%', #{string}, '%') or ADDRESS LIKE concat('%', #{string}, '%')")
	@Select({"<script>",
        "SELECT *from t_enterprise",
        "WHERE 1=1",
        "<when test='string != \"\" and string !=null'>",
        "and SIGN LIKE concat('%', #{string}, '%') or ADDRESS LIKE concat('%', #{string}, '%')",
        "</when>",
        "</script>"})
	public List<TEnterprise> selectByLike(@Param("string")String string);
	
	/**
	 * 查询库存数
	 * @param enterpriseId
	 * @return
	 */
	@Select("select STOCK_NUM FROM t_enterprise where ENTERPRISE_ID=#{enterpriseId}")
	public long selectNumById(long enterpriseId);
	

}
